package com.study.www.web;

import com.study.www.enums.ConfigEnum;
import com.study.www.model.Config;
import com.study.www.model.Result;
import com.study.www.model.mapper.ConfigRepository;
import com.study.www.service.QuartzTableService;
import lombok.extern.slf4j.Slf4j;
import org.quartz.CronScheduleBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.util.DateUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Description: :
 * @date :Create in  2018/2/24- 11:14
 * @Version: V1.0
 * @Modified By:
 **/
@Controller
@Slf4j
public class QuartzTable {

    private  static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd&HH:mm:ss");
    @Autowired
    ConfigRepository configRepository;
    @Autowired
    QuartzTableService tableService;


    @GetMapping(value = {"/table",""})
    public String table(ModelMap map) {
        List<Config> configs = configRepository.findAll();
        for (Config config : configs) {
            String message = ConfigEnum.findByMessage(config.getStatus());
            config.setStatus(message);
        }
        map.put("configs", configs);
        return "/table";
    }

    @GetMapping("/findConfig")
    @ResponseBody
    public Config findConfig(Long id) {
        Config one = configRepository.findOne(id);
        if (one.getStartTime() != null) {
            one.setStartTimeStr(simpleDateFormat.format(one.getStartTime()).replace("&","T"));
            one.setEndTimeStr(simpleDateFormat.format(one.getEndTime()).replace("&","T"));
        }
        return one;
    }

    @PostMapping("/updateStatus")
    @ResponseBody
    public Result updateStatus(Long id, String status) {
        Result result = new Result();
        result.setResult(false);
        try {
            tableService.update(id, status);
            result.setResult(true);
        } catch (Exception e) {
            result.setMsg(e.toString());
        }
        return result;
    }

    @PostMapping("/updateQuartz")
    @ResponseBody
    public Result updateQuartz(Long id, String cron) {
        Result result = new Result();
        result.setResult(false);
        try {
            CronScheduleBuilder cronSchedule = CronScheduleBuilder.cronSchedule(cron);
        } catch (Exception e) {
            result.setMsg("cron表达式有误！！");
            return result;
        }
        try {
            tableService.updateCron(id, cron);
            result.setResult(true);
        } catch (Exception e) {
            result.setMsg(e.toString());
        }
        return result;
    }


    @PostMapping("/saveQuartz")
    public String saveQuartz(Config config, HttpServletRequest request) {
        if (config.getStatus() == null || (!config.getStatus().equals(ConfigEnum.STATUS_START.getCode()))){
            config.setStatus(ConfigEnum.STATUS_STOP.getCode());
        }
        if (config.getCount() != null) {
            try {
                Date startTime = simpleDateFormat.parse(config.getStartTimeStr().replace("T", "&"));
                Date endTime = simpleDateFormat.parse(config.getEndTimeStr().replace("T", "&"));
                config.setStartTime(startTime);
                config.setEndTime(endTime);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        try {
            tableService.save(config);
        } catch (Exception e) {
        }
        return "redirect:/table";
    }
}
